package visiopuce.ui;

import java.text.SimpleDateFormat;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import visiopuce.objets.Famille;
import visiopuce.objets.Personne;
import visiopuce.service.FamilleServiceImpl;
import visiopuce.service.PersonneServiceImpl;

public class FamilleDialog extends Dialog {

	String titre;
	private Personne personne;
	private Integer idFamille;
	private Combo comboType;
	private Text tNom;
	private ContentProposalAdapter adapterNom;

	public FamilleDialog(Shell parentShell, String titre, Personne personne) {
		super(parentShell);
		this.titre = titre;
		this.personne = personne;
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(titre);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(1, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		comboType = new Combo(container, SWT.SINGLE | SWT.READ_ONLY);
		GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gridData.widthHint = 150;
		comboType.setLayoutData(gridData);
		comboType.setItems(new String[] { "Mère :", "Père :" });

		tNom = new Text(container, SWT.BORDER);

		tNom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		SimpleContentProposalProvider scp = new SimpleContentProposalProvider(null);
		try {
			adapterNom = new ContentProposalAdapter(tNom, new TextContentAdapter(), scp, KeyStroke.getInstance("Ctrl+Space"), null);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		adapterNom.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		tNom.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.keyCode == 262144) {
					setAutoCompletionNom(tNom.getText());
				}
			}

		});

		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Ok", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(255, 198);
	}

	@Override
	protected void okPressed() {
		String nom = null;
		String prenom = null;
		String nomPrenom = tNom.getText();
		String[] tablNomPrenom = nomPrenom.split(" # ");
		Personne personneLiee = null;
		if (tablNomPrenom.length == 3) {
			nom = tablNomPrenom[0];
			prenom = tablNomPrenom[1];
			String date = tablNomPrenom[2];
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			try {
				personneLiee = PersonneServiceImpl.getInstance().getPersonneByNomPrenomDateNaissance(nom, prenom, formatter.parse(date));
			} catch (java.text.ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		idFamille = FamilleServiceImpl.getInstance().getIdFamille(personne.getIdPersonne());

		if (idFamille == null || idFamille.equals(0)) {
			idFamille = personne.getIdPersonne();
		}
		Famille f = FamilleServiceImpl.getInstance().getIndividu(personne.getIdPersonne());
		if (f == null) {
			f = new Famille();
			f.setIdFamille(idFamille);
			f.setPersonne(personne);
			f.setMere(null);
			f.setPere(null);
			f.setSexe(0);
			f.setStatut(0);
		}
		if (comboType.getText().equals("Mère :")) {
			f.setMere(personneLiee);
		}
		if (comboType.getText().equals("Père :")) {
			f.setPere(personneLiee);
		}
		if (personne.getSexe() == "M") {
			f.setSexe(1);
		}
		if (personne.getSexe() == "F") {
			f.setSexe(2);
		}
		FamilleServiceImpl.getInstance().save(f);
		super.okPressed();
		super.close();
	}

	private void setAutoCompletionNom(String txt) {
		try {
			String[] defaultProposals = PersonneServiceImpl.getInstance().getNomPrenomFromString(txt);
			if (defaultProposals.length > 0) {
				((SimpleContentProposalProvider) adapterNom.getContentProposalProvider()).setProposals(defaultProposals);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}